﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_FangYuanBiaoService : It_FangYuanBiaoService
    {

        public readonly MyContext _Context;
        public List<t_FangYuanBiao> t_FangYuanBiao = new List<t_FangYuanBiao>();
        public t_FangYuanBiaoService(MyContext myContext)
        {
            _Context = myContext;
            //t_FangYuanBiao = _Context.t_FangYuanBiao.ToList();
        }

        public int Add(t_FangYuanBiao fangyuan)
        {
            _Context.t_FangYuanBiao.Add(fangyuan);
            return  _Context.SaveChanges();
        }

        public t_FangYuanBiao GetId(int id)
        {
            return  _Context.t_FangYuanBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_FangYuanBiao> GetList()
        {
            return  _Context.t_FangYuanBiao.ToList();
        }
        public List<t_FangYuanBiao> GetList1()
        {
            return _Context.t_FangYuanBiao.Where(x=>x.BeiZhu1=="启用").ToList();
        }
        public string ReturnWhere(t_FangYuanBiao t_FangYuanBiao)
        {
            string returnstr = "";
            if (t_FangYuanBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_FangYuanBiao.BeiZhu1 + ")";
            }
            if (t_FangYuanBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_FangYuanBiao.BeiZhu2 + ")";
            }
            if (t_FangYuanBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_FangYuanBiao.BeiZhu3 + ")";
            }
            if (t_FangYuanBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_FangYuanBiao.BeiZhu4 + ")";
            }
            if (t_FangYuanBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_FangYuanBiao.BeiZhu5 + ")";
            }
            if (t_FangYuanBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_FangYuanBiao.BeiZhu6 + ")";
            }
            if (t_FangYuanBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_FangYuanBiao.BeiZhu7 + ")";
            }
            if (t_FangYuanBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_FangYuanBiao.BeiZhu8 + ")";
            }
            if (t_FangYuanBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_FangYuanBiao.BeiZhu9 + ")";
            }
            if (t_FangYuanBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_FangYuanBiao.BeiZhu10 + ")";
            }
            if (t_FangYuanBiao.XiaoQuMingCheng != null)
            {
                returnstr = returnstr + " and XiaoQuMingCheng.Contains(" + t_FangYuanBiao.XiaoQuMingCheng + ")";
            }
            if (returnstr.Length > 0)
            {
                return returnstr.Substring(4);
            }
            else
            {
                return "";
            }
        }

        public t_FangYuanBiao GetIdAsync(int id)
        {
            return  _Context.t_FangYuanBiao.FirstOrDefault(x => x.Id == id);
        }

       
         
        public t_FangYuanBiao GetModel(t_FangYuanBiao t_FangYuanBiao)
        {
            return _Context.t_FangYuanBiao.FirstOrDefault(x => x.XiaoQuMingCheng == t_FangYuanBiao.XiaoQuMingCheng);
        }

        public List<t_FangYuanBiao> GetListXiaoQu(string yj)
        {
            return _Context.t_FangYuanBiao.Where(yj).ToList();
        }

        public string GetListWithFenYe(t_FangYuanBiao t_FangYuanBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_FangYuanBiao);
            var list = new List<t_FangYuanBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_FangYuanBiao.Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_FangYuanBiao.ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.Id).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.Id).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.XiaoQuMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.XiaoQuMingCheng).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
